﻿////页面加载
//$(document).ready(function () {
//    $("#btnSave").bind("click", btnSave_click);
//    //$("#btnCancel").bind("click", btnCancel_click);
//    Count_Total();
//})



$(document).on("pageinit", "#mainPage", function () {

    //注册删除事件: 从左向右滑动
    // $("#olOrder #liData").bind("swipeleft", btnDel_click);
    $("#olOrder #liData a[data-icon='delete']").bind("click", btnDel_click);
    $("#btnSave").bind("click", btnSave_click);
    $("#olOrder input[name='btnMinus']").bind("click", btnMinus_click);
    $("#olOrder input[name='btnPlus']").bind("click", btnPlus_click);

    //初始化日历控件
    // jquery mobile 初始化完毕后的事件
    var opt = {
        preset: 'datetime', //日期
        theme: 'jqm', //皮肤样式
        display: 'modal', //显示方式 
        mode: 'scroller', //日期选择模式
        dateFormat: 'yy-mm-dd', // 日期格式
        timeFormat:'HH:ii:ss', //时间格式
        setText: '确定', //确认按钮名称
        cancelText: '取消',//取消按钮名籍我
        dateOrder: 'yymmdd', //面板中日期排列格式
        timeWheels:'HHii',//时间面板
        dayText: '日', monthText: '月', yearText: '年', hourText: '时', minuteText: '分',  //面板中年月日文字
        endYear: 2099//结束年份
    };

    $('input:jqmData(role="datebox")').mobiscroll(opt);
});

function btnSave_click() {


    var v_Password = $("#Password").val();
    if (v_Password == "") {
        alert("请输入支付密码！");
        return;
    }
    var v_Phone = $("#Buyer_Phone").val();
    if (v_Phone == "") {
        alert("请输联系电话！");
        return;
    }
    var v_appointment = $("#Buyer_appointment").val();
    if (v_appointment == "") {
        alert("请输入预约时间！");
        return;
    }

    var v_Note = $("#Buyer_Note").val();
   
    //提取购买产品和数量
    var totalList = $("#olOrder #liData");
    var v_buyItems = "";
    $.each(totalList, function (i, item) {
        var v_ProID = $(item).find("input[name='Pro_ID']").val();
        var v_Quantity = $(item).find("input[name='Quantity']").val();
        v_buyItems += v_ProID + "," + v_Quantity + "|";
        //v_buyItems += parseFloat($(item).html());
    });
    if (v_buyItems == "")
    {
        alert("您的购物车是空的!");
        return;
    }

    var v_Buyer = $("#Buyer").val();

    if (confirm("你确使提交订单？")) {
        //异步提交表单,表单中input元素必须设置name 属性
        $.post("/WeChatProduct/PayBill", { buyItems: v_buyItems, buyer: v_Buyer, password: v_Password, phone: v_Phone, note: v_Note, appointment: v_appointment }, function (result) {
            if (result.code =="OK") {
                //订单购买成功,转到成功页面 
                $("#divOk").show();
                $("#divOrder").hide();
            }
            else {
                alert(result.msg);
            }
        }, "Json");
    }

}


////改变数量，计算小计
//function Quantity_onchange(e) {
//    var quantity = $(e).val();
//    var price = $(e).parent().parent().parent().find("#pPrice").html();
//    $(e).parent().parent().parent().find("#total").html(price * quantity);

//    //刷新总价
//    Count_Total();
//}


function btnDel_click() {
    var v_productId = $(this).parent().find("input[name='Pro_ID']").val();
    var v_li = $(this).parent();
    if (confirm("你确使删除该项？")) {
        //异步提交表单,表单中input元素必须设置name 属性
        $.post("/WeChatProduct/DeleteFromShoppingCart", { productId: v_productId }, function (result) {
            if (result="OK") {
                //刷新页面
                v_li.remove();
                $("#olOrder").listview('refresh');
                Count_Total();//刷新总价
            }
            else {
                alert(result);
            }
        });
    }
}



function btnMinus_click()
{
    var quantity = $(this).parent().find("#Quantity");
    var curValue = parseInt(quantity.val()) - 1;
    if (curValue < 1) {
        curValue = 1;
    }
    quantity.val(curValue);
    
    //更新小计:
    var v_price = $(this).parent().parent().find("#pPrice").html();
    v_price = parseFloat(v_price);

    var v_total = $(this).parent().parent().find("#total");
    v_total.html(accMul(v_price,curValue));

    Count_Total();//刷新总计
}

function btnPlus_click()
{
    var quantity = $(this).parent().find("#Quantity");
    var curValue = parseInt(quantity.val()) + 1;
    quantity.val(curValue);

    //更新小计:
    var v_price = $(this).parent().parent().find("#pPrice").html();
    v_price = parseFloat(v_price);

    var v_total = $(this).parent().parent().find("#total");
    v_total.html(accMul(v_price,curValue));
    Count_Total();//刷新总计
}

//js 乘法
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

// js 加法
function accAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return (arg1 * m + arg2 * m) / m
}

//刷新总计
function Count_Total() {
    var totalList = $("#olOrder li span[id='total']");
    var v_orderTotal = 0;
    $.each(totalList, function (i, item) {
        // v_orderTotal += parseFloat($(item).html());
        var oneItemTotal = parseFloat($(item).html());
        v_orderTotal = accAdd(v_orderTotal, oneItemTotal);
    });
    $("#OrderTotal").html(v_orderTotal);
}


function btnCancel_click() {
    window.history.go(-1);
}
